//
//  GQHPasswordController.m
//  Xanthium
//
//  Created by GuanQinghao on 05/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHUserModel.h"

#pragma mark Object

#pragma mark View
#import "GQHPasswordView.h"

#pragma mark Controller
#import "GQHPasswordController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHPasswordController () {
    
#pragma mark Data
    
#pragma mark View
    /// 自定义根视图
    GQHPasswordView *rootView;
}

@end


@implementation GQHPasswordController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHPasswordView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"修改密码";
    [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    NSString *passwordString = rootView.qh_passwordTextField.text;
    NSString *againString = rootView.qh_againTextField.text;
    
    if ([passwordString isEqualToString:againString]) {
        
        // 修改密码接口
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setValue:self.qh_mobile forKey:@"phone"];
        [parameters setValue:passwordString forKey:@"passWord"];
        [parameters setValue:againString forKey:@"password1"];
        [parameters setValue:USER_GET_ID forKey:@"userId"];
        
        
        [GQHUserModel qh_changePasswordWith:parameters success:^(BOOL status, NSNumber *code, NSString *message) {
            
            if (status) {
                
                // 返回上两级页面
                NSUInteger index = [[self.navigationController viewControllers] indexOfObject:self];
                UIViewController *controller = [[self.navigationController viewControllers] objectAtIndex:index - 2];
                [self.navigationController popToViewController:controller animated:YES];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
        }];
    } else {
        
        [self showToastWithText:@"两次密码不一致，请重新输入"];
        rootView.qh_passwordTextField.text = @"";
        rootView.qh_againTextField.text = @"";
        [rootView.qh_passwordTextField becomeFirstResponder];
    }
}
#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
